


//
//  ZRMediaViewController.m
//  ZQLearn
//
//  Created by admin on 2019/4/17.
//  Copyright © 2019 admin. All rights reserved.
#import "RMediaSearchResulVC.h"
#import "ZSearchResultVC.h"
#import "ZRMediaViewController.h"
#import "ZRMediaSubViewController.h"
#import "ZJScrollPageView.h"
#import "NewsModel.h"
#import "StudyHardViewController.h"
#import "SearchBar.h"
@interface ZRMediaViewController ()<ZJScrollPageViewDelegate>
/**
 页面分栏的标题
 */
@property (strong,nonatomic) NSArray<NSString *>*titles;

/**
 横向分页滑动组件
 */
@property (strong,nonatomic) ZJScrollPageView *scrollPageView;

@property (strong,nonatomic) NSMutableArray *dataArray;

@property (strong,nonatomic) NSMutableArray *titleArray;

@property (strong,nonatomic) SearchBar *searchBar;
@end

@implementation ZRMediaViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     self.searchBar.hidden = false;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.searchBar.hidden = YES;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    //背景蓝
//    self.navigationController.navigationBar.barTintColor = NColorFrom16RGB(0x23b7e5);
    [self.navigationItem setItemWithTitle:@"融媒体" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    self.dataArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    [self loadData];
    
    self.view.backgroundColor = kWhiteColor;
    [self addSearchBar];
    
    // Do any additional setup after loading the view.
}

#pragma mark - 添加搜索框
- (void)addSearchBar{
    SearchBar *bar = [[[NSBundle mainBundle]loadNibNamed:@"SearchBar" owner:self options:nil]lastObject];
    bar.frame = CGRectMake(35, 7, ScreenWidth-70, 30);
    bar.layer.cornerRadius = 15.f;
    bar.layer.masksToBounds = YES;
    self.searchBar = bar;
    //    self.navigationItem.titleView = bar;
    [self.navigationController.navigationBar addSubview:bar];
     [bar.barBtn setTitle:@"请输入关键字搜索资讯" forState:UIControlStateNormal];
    [bar.barBtn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
}

///搜索动作
- (void)searchAction:(UIButton *)btn{
    RMediaSearchResulVC *ss = [RMediaSearchResulVC new];
//    ss.flag = @"med";
    [self.navigationController pushViewController:ss animated:YES];
}

- (void)setTabWithTitles:(NSArray *)titles{
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    style.scrollTitle = YES;
    style.scrollLineColor = NColorFrom16RGB(0x23b7e5);
    style.selectedTitleColor = NColorFrom16RGB(0x23b7e5);
    style.segmentViewBounces = YES;
    style.titleFont = kFont15;
    style.adjustCoverOrLineWidth = YES;
    style.autoAdjustTitlesWidth = YES;
    style.segmentHeight = 40;
    self.titles = titles;
    // 初始化
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth, ScreenHeight - 20.0) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [self.view addSubview:_scrollPageView];
    ///
//    [_scrollPageView setSelectedIndex:3 animated:NO];
    
}

- (void)loadData{
    [ZNetworkService getRequestWithBaseUrl:kNewsTabTitleUrl setParameters:@{} success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                NewsModel *model = [NewsModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
                NSString *title = model.name;
                [self.titleArray addObject:title];
            }
            [self setTabWithTitles:self.titleArray];
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
}

#pragma mark ZJPhotoScrollViewDelegate
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    NewsModel *model = self.dataArray[index];
    
    if (index==0) {
        
        ///拿到这个id 进行当前对应列表的数据请求
        [UserModel shareInstance].newsId = model.ID;
        YYLog(@"%@",model.ID);
        StudyHardViewController *childVc = (StudyHardViewController *)reuseViewController;
        if (!childVc) {
            childVc = [[StudyHardViewController alloc] init];
        }
        return childVc;
        
    }else{
        //    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
        ZRMediaSubViewController *childVc = (ZRMediaSubViewController *)reuseViewController;
        if (!childVc) {
            
            childVc = [[ZRMediaSubViewController alloc] init];
            if (self.dataArray.count>0) {
                childVc.datas = self.dataArray;
            }
            
        }
        return childVc;
    }
    return nil;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;
}


@end
